package com.tianqu.tms.www.cardect.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.DectCodes;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.www.cardect.viewhelper.CarWDIpadTopListViewHelper;
/**
 * 待检测（IPAD）车辆信息详细页Action
 * 
 * @author tianshifu
 *
 */
public class CarWDIpadDetailAction extends TmsAction {
	private Logger logger = Logger.getLogger(CarWDIpadDetailAction.class);//log
	private static final String CARSERIAL = "carSerial";//页面传过来的车辆ID
	public final static String SOURSE = "sourse";

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String carSerial = request.getParameter(CARSERIAL);
		
		//通过车辆ID查询出车辆基本信息放到viewhelp中
		TmsDetectInfo entity = TmsDetectInfo.getInstance();
		
		entity = entity.getWDIpadCarInfoBySerial(carSerial,1);
		
		CarWDIpadTopListViewHelper view = new CarWDIpadTopListViewHelper();
		
		setViewHelpFromEntity(view,entity);
		
		String sourse = (String)uc.getAttribute(SOURSE);
		if(sourse.equals("服务器端"))
		{
			view.setStatus("待检测(IPAD) - 未同步到客户端");
		}
		if(sourse.equals("客户端"))
		{
			view.setStatus("待检测(IPAD) - 已同步到客户端");
		}
		
		request.setAttribute(TmsConstants.VIEWHELP, view);
		uc.setAttribute(TmsConstants.VIEWHELP, view);//用于删除确认页调用
//		uc.removeAttribute(SOURSE);//进入到详细页将车辆状态清空
		logger.debug(this.getClass().getName() + "#doAction was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	
	/**
	 * 将entity值放到viewhelp中
	 * @throws TmsDaoException 
	 */
	private CarWDIpadTopListViewHelper setViewHelpFromEntity(CarWDIpadTopListViewHelper view,TmsDetectInfo entity) throws TmsDaoException
	{
		view.setCarSerial(entity.getCarSerial());
		view.setCarName(entity.getCarName());
		view.setCarLicense(entity.getCarLicense());
		view.setRegDate(entity.getRegDate());
		view.setCarCheckInsur(entity.getCarCheckInsur());
		view.setTrafficInsur(entity.getTrafficInsur());
		Codes tmsCode = Codes.getInstance();
		view.setColor(tmsCode.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR,Integer.parseInt(entity.getColor())));
		view.setUpdateTime(entity.getUpdateTime());
		view.setExhaust(StringUtil.getNewExhaust(entity.getExhaust()));//毫升转成升+“L”
		DectCodes code = DectCodes.getInstance();

		view.setGearbox(code.getItemValueByItemIdAndItemCode(DectCodes.GEARBOX_TYPE, String.valueOf(entity.getGearbox()))); 
		view.setFuelDict(code.getItemValueByItemIdAndItemCode(DectCodes.FUEL_TYPE, String.valueOf(entity.getFuelDict()))); 
		view.setNature(code.getItemValueByItemIdAndItemCode(DectCodes.NATURE_TYPE, String.valueOf(entity.getNature()))); 
		view.setSource(code.getItemValueByItemIdAndItemCode(DectCodes.CAR_SOURSE, String.valueOf(entity.getSource())));

		return view;
	}

}
